package cn.kgc.controllers;

import cn.kgc.pojo.MembersInfo;
import cn.kgc.service.MembersInfoService;
import com.github.pagehelper.PageHelper;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("mem")
public class MembersInfoController {

    @Autowired
    MembersInfoService membersInfoService;

    @RequestMapping("find")
    public String getMems(Model model, @RequestParam(value="id", required = false, defaultValue="0")Integer id){
        System.out.println("id:" + id);
        List<MembersInfo> membersInfoList = membersInfoService.getMembersInfoList(id);
        System.out.println("数据如下:");
        for (MembersInfo membersInfo : membersInfoList) {
            System.out.println(membersInfo.toString());
        }
       model.addAttribute("membersInfoList", membersInfoList);
        return "showmem";
    }

    @RequestMapping("findOne")
    public String getMem(Model model, @RequestParam(value="id", required = false, defaultValue="0")Integer id){
        List<MembersInfo> membersInfoList = membersInfoService.getMembersInfoList(id);
        MembersInfo membersInfo = membersInfoList.get(0);
        System.out.println("数据:" + membersInfo.toString());
        model.addAttribute("member", membersInfo);
        return "showOne";
    }

    @RequestMapping("update")
    public void update(@RequestParam("name")String name,
                         @RequestParam("sex")String sex,
                         @RequestParam("age")Integer age,
                         @RequestParam("address")String address,
                         @RequestParam("email")String eamil,
                         @RequestParam("id")Integer id, Model model,
                         HttpServletResponse response){
        MembersInfo membersInfo = new MembersInfo();
        membersInfo.setMid(id);
        membersInfo.setAge(age);
        membersInfo.setMaddress(address);
        membersInfo.setMemail(eamil);
        membersInfo.setMname(name);
        membersInfo.setMgender(sex);
        int result = membersInfoService.updateMembersInfo(membersInfo);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String msg = null;
        if (result > 0) {
            msg = "<script type='text/javascript'>alert('更新成功！');window.location.href='../mem/find'</script>";
        }else {
            msg = "<script type='text/javascript'>alert('更新失败！');history.go(-1);</script>;";
        }
        writer.print(msg);
        writer.flush();
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(@RequestParam("id")Integer id, Model model,
                       HttpServletResponse response){
        int result = membersInfoService.deleteMembersInfoById(id);

        if(result > 0){
            return "1";
        }else{
            return "0";
        }
    }






}
